<?php

/**
 * ECSHOP 管理员信息以及权限管理程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: privilege.php 17217 2011-01-19 06:29:08Z liubo $
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table("school"), $db, 'region_id', 'region_name');

if ($_REQUEST['act'] == 'list')
{
    $sql = "select region_id, region_name, add_money, overall_money, number from ".$ecs->table('school')." where region_type=2";
    $school_list = $db->getAll($sql);

    $smarty->assign('school_list',   $school_list);
    $smarty->assign('ur_here',      $_LANG['school_list_money']);

    $school_list = school_list();

    foreach($school_list['school_list'] as $key=>$vo){
        $id=$vo['region_id'];
        $sql= "select c.region_name ,sum(s.rund_money) from ".$GLOBALS['ecs']->table('school')." as c left join ".$GLOBALS['ecs']->table('order_info')." as s on c.region_id= s.school_name where c.region_id=".$id;
        $school = $GLOBALS['db']->getRow($sql);
        $school_list['school_list'][$key]['all_money'] = $school['sum(s.rund_money)'];
    }

    $smarty->assign('school_list',    $school_list['school_list']);
    $smarty->assign('filter',       $school_list['filter']);
    $smarty->assign('record_count', $school_list['record_count']);
    $smarty->assign('page_count',   $school_list['page_count']);
    $smarty->assign('full_page',    1);
    $smarty->assign('sort_user_id', '<img src="images/sort_desc.gif">');

    assign_query_info();
   $smarty->display('school_list_money.htm');
}

elseif ($_REQUEST['act'] == 'edit_add_money')
{
    check_authz_json('school_list_money');

    $region_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $val = empty($_REQUEST['val']) ? 0 : floatval($_REQUEST['val']);

    if ($val < 0)
    {
        make_json_error('额外捐赠金额不能为负数！');
    }

    if ($exc->edit("add_money = '$val'", $region_id))
    {
        $region_name = $exc->get_name($region_id);
        //admin_log(addslashes($recommend_name), 'edit', 'user_rank');
        clear_cache_files();
        make_json_result($val);
    }
    else
    {
        make_json_error($val);
    }
}
elseif ($_REQUEST['act'] == 'edit_overall_money')
{
    check_authz_json('school_list_money');

    $region_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $val = empty($_REQUEST['val']) ? 0 : floatval($_REQUEST['val']);

    if ($val < 0)
    {
        make_json_error('捐赠金额不能为负数！');
    }

    if ($exc->edit("overall_money = '$val'", $region_id))
    {
        $region_name = $exc->get_name($region_id);
        //admin_log(addslashes($recommend_name), 'edit', 'user_rank');
        clear_cache_files();
        make_json_result($val);
    }
    else
    {
        make_json_error($val);
    }
}
elseif ($_REQUEST['act'] == 'edit_number')
{
    check_authz_json('school_list_money');

    $region_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $val = empty($_REQUEST['val']) ? 0 : floatval($_REQUEST['val']);

    if ($val < 0)
    {
        make_json_error('捐赠次数不能为负数！');
    }

    if ($exc->edit("number = '$val'", $region_id))
    {
        $region_name = $exc->get_name($region_id);
        //admin_log(addslashes($recommend_name), 'edit', 'user_rank');
        clear_cache_files();
        make_json_result($val);
    }
    else
    {
        make_json_error($val);
    }
}
/*------------------------------------------------------ */
//-- ajax返回用户列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $school_list = school_list();

    foreach($school_list['school_list'] as $key=>$vo){
        $id=$vo['region_id'];
        $sql= "select c.region_name ,sum(s.rund_money) ,c.overall_money from ".$GLOBALS['ecs']->table('school')." as c left join ".$GLOBALS['ecs']->table('order_info')." as s on c.region_id= s.school_name where c.region_id=".$id;
        $school = $GLOBALS['db']->getRow($sql);
        $school_list['school_list'][$key]['all_money'] = $school['sum(s.rund_money)'];
    }

    $smarty->assign('school_list',    $school_list['school_list']);
    $smarty->assign('filter',       $school_list['filter']);
    $smarty->assign('record_count', $school_list['record_count']);
    $smarty->assign('page_count',   $school_list['page_count']);

    $sort_flag  = sort_flag($school_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('school_list_money.htm'), '', array('filter' => $school_list['filter'], 'page_count' => $school_list['page_count']));
}

elseif($_REQUEST['act'] == 'edit'){
    $id = $_REQUEST['id'];
    $sql= "select c.region_name ,sum(s.rund_money) ,c.add_money ,c.parent_id ,c.overall_money ,c.img_logo, number from ".$GLOBALS['ecs']->table('school')." as c left join ".$GLOBALS['ecs']->table('order_info')." as s on c.region_id= s.school_name where c.region_id=".$id;
    $school = $GLOBALS['db']->getRow($sql);
    $pid = $school['parent_id'];

    $smarty->assign('school_name',$school['region_name']);
    $smarty->assign('region_id',$id);
    $smarty->assign('ur_here',          '管理捐赠学校');
    $smarty->assign('action_link',      array('text' => $_LANG['school_list_money'], 'href'=>'school_list_money.php?act=list'));
    $smarty->assign('add_money',$school['add_money']);
    $smarty->assign('overall_money',$school['overall_money']);
    $smarty->assign('img_logo',$school['img_logo']);
    $smarty->assign('number',$school['number']);
    $smarty->assign('rund_money',$school['sum(s.rund_money)']);
    $sql = "select c.region_name, s.region_name as old_parent_name from ".$GLOBALS['ecs']->table('school')." as c left join ".$GLOBALS['ecs']->table('school')." as s on c.parent_id=s.region_id where c.region_id=".$pid;
    $parent = $GLOBALS['db']->getRow($sql);
    $smarty->assign('parent_name',$parent['region_name']);
    $smarty->assign('old_parent_name',$parent['old_parent_name']);
    $smarty->display('school_edit.htm');
}

elseif($_REQUEST['act']=='update'){
    /* 检查图片：如果有错误，检查尺寸是否超过最大值；否则，检查文件类型 */
    if (isset($_FILES['img_logo']['error'])) // php 4.2 版本才支持 error
    {
        // 最大上传文件大小
        $php_maxsize = ini_get('upload_max_filesize');
        $htm_maxsize = '2M';

        // 商品图片
        if ($_FILES['img_logo']['error'] == 0)
        {
            if (!$image->check_img_type($_FILES['img_logo']['type']))
            {
                sys_msg('LOGO图片格式不正确！', 1, array(), false);
            }
        }
        elseif ($_FILES['img_logo']['error'] == 1)
        {
            sys_msg(sprintf('LOGO图片文件太大了，无法上传。', $php_maxsize), 1, array(), false);
        }
        elseif ($_FILES['img_logo']['error'] == 2)
        {
            sys_msg(sprintf('LOGO图片文件太大了，无法上传。', $htm_maxsize), 1, array(), false);
        }

    }
    /* 4.1版本 */
    else
    {
        // 商品图片
        if ($_FILES['img_logo']['tmp_name'] != 'none')
        {
            if (!$image->check_img_type($_FILES['img_logo']['type']))
            {
                sys_msg('LOGO图片格式不正确!', 1, array(), false);
            }
        }
    }
    // 如果上传了商品图片，相应处理
    if (($_FILES['img_logo']['tmp_name'] != '' && $_FILES['img_logo']['tmp_name'] != 'none'))
    {
        if ($_REQUEST['id'] > 0)
        {
            /* 删除原来的图片文件 */
            $sql = "SELECT img_logo " .
                " FROM " . $ecs->table('school') .
                " WHERE region_id = '$_REQUEST[id]'";
            $row = $db->getRow($sql);

            if ($row['img_logo'] != '' && is_file('../' . $row['img_logo']))
            {
                @unlink('../' . $row['img_logo']);
            }

        }
            $original_img   = $image->upload_image($_FILES['img_logo']); // 原始图片
            $img_logo = $original_img;

        if ($original_img === false)
        {
            sys_msg($image->error_msg(), 1, array(), false);
        }
        // 如果系统支持GD，缩放商品图片，且给商品图片和相册图片加水印
        if ( $image->gd_version() > 0 && $image->check_img_function($_FILES['img_logo']['type']))
        {
            // 加水印
            if (intval($_CFG['watermark_place']) > 0 && !empty($GLOBALS['_CFG']['watermark']))
            {
                if ($image->add_watermark('../'.$img_logo,'',$GLOBALS['_CFG']['watermark'], $GLOBALS['_CFG']['watermark_place'], $GLOBALS['_CFG']['watermark_alpha']) === false)
                {
                    sys_msg($image->error_msg(), 1, array(), false);
                }
            }
        }
    }

    $add_money = !empty($_POST['add_money']) ? $_POST['add_money'] : 0;
    $overall_money = !empty($_POST['overall_money']) ? $_POST['overall_money'] : 0;
    $number = !empty($_POST['number']) ? $_POST['number'] : 0;
    $id  = $_REQUEST['id'];
    $sql = "UPDATE " . $ecs->table('school') . " SET " .
        "add_money = '$add_money', "." overall_money = '$overall_money', "." number = '$number'";
    if ($img_logo)
    {
        $sql .= ", img_logo = '$img_logo' ";
    }
    $sql .= " where region_id=".$id;
    $db->query($sql);

    $link=array();
    $link[0] = array('href' => 'school_list_money.php?act=edit&id=' . $id, 'text' => '返回上一层');
    sys_msg('修改成功！',0,$link);

}
/**
 *  返回用户列表数据
 *
 * @access  public
 * @param
 *
 * @return void
 */
function school_list()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keywords'] = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }


        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'region_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

        $ex_where = ' WHERE region_type=2 ';
        if ($filter['keywords'])
        {
            $ex_where .= " AND region_name LIKE '%" . mysql_like_quote($filter['keywords']) ."%'";
        }

        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('school') . $ex_where);

        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT region_id, region_name, add_money, overall_money, number FROM " . $GLOBALS['ecs']->table('school') . $ex_where .
            " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
            " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $school_list = $GLOBALS['db']->getAll($sql);

    $count = count($school_list);

    $arr = array('school_list' => $school_list, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>